//
//  CTFinanceTransformInVC.m
//  digitalCurrency
//
//  Created by apple on 2019/6/12.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CTFinanceTransformInVC.h"
#import "CETransformCell.h"
#import "HomeNetManager.h"
#import "CTFinanceTransformInListVC.h"

@interface CTFinanceTransformInVC ()
@property (nonatomic, strong) UITextField *amountTextField;
@property (nonatomic, strong) UIView *footView;
@property (nonatomic, strong) UILabel *tipsLabel;

@end

@implementation CTFinanceTransformInVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewTitle.text = LocalizationKey(@"Transfer-in");
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.rowHeight = 80;
    [self.tableView registerClass:[CETransformCell class] forCellReuseIdentifier:@"litcell"];
    
//    [self rightButton];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)RighttouchEvent {
    CTFinanceTransformInListVC *vc = [[CTFinanceTransformInListVC alloc] init];
    vc.unit = self.unit;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)rightButton {
    UIButton * issueButton = [UIButton buttonWithType:UIButtonTypeSystem];
    issueButton.frame = CGRectMake(0, 0, 80, 24);
    issueButton.titleLabel.font = [UIFont systemFontOfSize:14];
    issueButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
    [issueButton setTitle:LocalizationKey(@"Roll-in list") forState:UIControlStateNormal];
    [issueButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [issueButton addTarget:self action:@selector(RighttouchEvent) forControlEvents:UIControlEventTouchUpInside];
    //添加到导航条
    UIBarButtonItem *leftBarButtomItem = [[UIBarButtonItem alloc]initWithCustomView:issueButton];
    self.navigationItem.rightBarButtonItem = leftBarButtomItem;
}

- (void)confirmClick {
    kPreventRepeatClickTime(1);

    NSString *amont = self.amountTextField.text;
    if (amont.length == 0) {
        return;
    }
    if ([amont floatValue] <= 0) {
        return;
    }
    [self.view endEditing:YES];

    __weak typeof(self)weakself = self;
    [EasyShowLodingView showLodingText:LocalizationKey(@"loading")];
    NSDictionary *dic = @{@"coinId":self.unit,@"amount":amont};
    [HomeNetManager doFinancingsWithInfo:dic CompleteHandle:^(id resPonseObj, int code) {
        weakself.amountTextField.text = @"";
        [EasyShowLodingView hidenLoding];
        NSLog(@"resPonseObj:%@",resPonseObj);
        NSInteger codeInt = [resPonseObj[@"code"] integerValue];
        if (code) {
            if (codeInt == 0) {
                [[NSUserDefaults standardUserDefaults] setObject:amont forKey:@"balanceCnt"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                [weakself.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakself.navigationController popViewControllerAnimated:YES];
                });
            }
            else if (codeInt == -1000001 ){
                [ShowLoGinVC showLoginVc:self withTipMessage:resPonseObj[MESSAGE]];
            }
            else {
                [weakself.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
        }
        else {
            [weakself.view makeToast:[[ChangeLanguage bundle] localizedStringForKey:@"noNetworkStatus" value:nil table:@"English"] duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    CETransformCell *cell = [CETransformCell createTableView:tableView];
   
    self.amountTextField = cell.textField;
    
    cell.textField.placeholder = [NSString stringWithFormat:@"%@: %@",LocalizationKey(@"Can be transferred"),self.balance];
    [cell.scanbtn setTitle:LocalizationKey(@"All") forState:UIControlStateNormal];
    [cell.scanbtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    @weakify(self);
    [[cell.scanbtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        kPreventRepeatClickTime(1);
        cell.textField.text = self.balance;
    }];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 440;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return self.footView;
}

- (UIView *)footView {
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 440)];
        _footView.backgroundColor = [UIColor whiteColor];
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        confirmBtn.layer.cornerRadius = 5;
        confirmBtn.backgroundColor = [UIColor colorWithHEX:maincolor];
        [confirmBtn setTitle:LocalizationKey(@"Confirm") forState:UIControlStateNormal];
        [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        confirmBtn.titleLabel.font = kFont(18);
        confirmBtn.frame = CGRectMake(23, 56, SCREEN_WIDTH-46, 44);
        [confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
        [_footView addSubview:confirmBtn];
        
        UILabel *tipslabel = [[UILabel alloc] init];
        NSString *LanguageChan = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
        if (LanguageChan || ![LanguageChan isEqualToString:@"English"]) {
            tipslabel.frame = CGRectMake(23, 125, SCREEN_WIDTH-46, 240);
        }
        else {
            tipslabel.frame = CGRectMake(23, 125, SCREEN_WIDTH-46, 315);
        }
        [_footView addSubview:tipslabel];
        tipslabel.font = [UIFont systemFontOfSize:12];
        tipslabel.numberOfLines = 0;
        tipslabel.textColor = [UIColor colorWithHEX:@"#9FB0BD"];
        NSString *str0 = LocalizationKey(@"Financial rules");
        NSString *str2 = LocalizationKey(@"financerule2");
//        NSString *str3 = LocalizationKey(@"financerule3");
        NSString *str4 = LocalizationKey(@"financerule4");

        NSString *str  = [NSString stringWithFormat:@"%@\n%@\n%@",str0,str2,str4];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
                                             initWithString:str attributes:@{
                                            NSKernAttributeName:@(1),
                                            }];
        [string addAttribute:NSFontAttributeName value:kFont(15) range:NSMakeRange(0, str0.length)];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 5; // 调整行间距
        NSRange range = NSMakeRange(0, [string length]);
        [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
        tipslabel.attributedText = string;
    }
    
    return _footView;
}

-(void)dealloc{
    NSLog(@"%s",__func__);
}

@end
